require(['init'], function () {
  defaule();
  var _id;
  function defaule(){
      if (!localStorage.lang) {
          localStorage.setItem('lang', 'zh');
      }
      var dayNamesShort;
      var titleFormat;

      if (localStorage.lang && localStorage.lang == 'zh') {
          dayNamesShort = ["周日  ", "周一 ", "周二 ", "周三 ", "周四 ", "周五  ", "周六 "];
          titleFormat='yyyy年MM月'
      }
      else {
          dayNamesShort = ["Sunday  ", "Monday ", "Tuesday ", "Wednesday ", "Thursday ", "Friday  ", "Saturday "];
          titleFormat='yyyy Year MM'
      }
    var res="";
     var initialLangCode = 'en';
    $('#calendar').fullCalendar({
          header: {
            left: 'prev,next',
            center: 'title',
            right: 'today'
        },
        titleFormat:titleFormat,
        dayNamesShort: dayNamesShort,
        defaultDate: '',
        lang: 'zh-cn',
        buttonIcons: false, // show the prev/next text
        weekMode: 'liquid',
        events:function (start,end,callback) {
            if (localStorage.lang && localStorage.lang == 'en') {
                $(".fc-header-title h4").html($(".fc-header-title h4").text()+" Month")
                $('.fc-button-today').html('today')
            }
        }
        // events:function(start,end,callback){
        //     var id=$('input[name="id"]').val();
        //     $.ajax({
        //      // url:"/api/schedules?teacheruid=3&begintime=2017-05-01&endtime=2017-05-08&status=1"
        //      //这里就
        //       url:"/api/schedules",
        //       // data:{
        //       //   teacheruid:id,
        //       //   status:1
        //       // },
        //       type:"get",
        //       success:function(data){
        //       //   console.log(data)
        //       //  var event=[];
        //       //  $.each(data.data,function(i,t){
        //       //  event.push({
        //       //           title: t.period+' '+t.name,
        //       //           //title:t.name,
        //       //           start: t.begintime, // will be parsed
        //       //           end:t.endtime,
        //       //           id:t.id,
        //       //           backgroundColor:"#33c7c6",
        //       //           textColor:"#fff"
        //        //
        //       //     });
        //       //   });
        //       //    callback(event);
        //       //  for(var i=0; i<event.length;i++){
        //       //    var _id=event[i].id;
        //        //
        //       //  }
        //       }
        //    });
        //
        // }
    })
  }
  function getappointrecords() {
      ajaxMethod({
          url: '/api/teachers',
          type: 'get',
          dataType: 'json',
          success: function (data) {
            var $studentC = $('.studentC').html("")
            $.each(data, function (index, item) {
                function getDurationMinute(begindate, enddate) {
                    if (begindate != null || enddate != null) {
                        var beginTime = +new Date(begindate.replace('T', ' '));
                        var endTime = +new Date(enddate.replace('T', ' '));
                        return (endTime - beginTime) / (60 * 1000) + '分钟';
                    } else {
                        return '--';
                    }
                }

                var id = item.teacheruid;
                var $tr = $('<tr class="enroll" data-uid=' + id + ' style="height:37px"></tr>').appendTo($studentC);
                var $td = $('<td ></td>').appendTo($tr);
                var $detailedinformation = $('<a  data-uid='+id+'   data-name='+item.lastname+' style="">'+item.lastname+'</a>').appendTo($td);
                $tr.append('<td>' + item.mobile + '</td>')
                $tr.find('td').css('font-size', '13px');
                $tr.on('click',function(){
                $tr.addClass('active').siblings().removeClass('active');
                  var  name=$tr.find($detailedinformation).attr('data-name');
                  var studentName=$('#studentName').html(name);
                  if($("#calendar").html!=""){
                    $("#calendar").html("");
                    $('input[name="id"]').val(item.teacheruid);
                    getfullCalendar();
                  }
                })
              })
          }

      })

  }

 getappointrecords();
 //日历展示
 var _id;
 function getfullCalendar(){
   var res="";
    var initialLangCode = 'en';
     if (!localStorage.lang) {
         localStorage.setItem('lang', 'zh');
     }
     var dayNamesShort;
     var titleFormat;

     if (localStorage.lang && localStorage.lang == 'zh') {
         dayNamesShort = ["周日  ", "周一 ", "周二 ", "周三 ", "周四 ", "周五  ", "周六 "];
         titleFormat='yyyy年MM月'
     }
     else {
         dayNamesShort = ["Sunday  ", "Monday ", "Tuesday ", "Wednesday ", "Thursday ", "Friday  ", "Saturday "];
         titleFormat='yyyy Year MM'
     }
   $('#calendar').fullCalendar({
         header: {
           left: 'prev,next',
           center: 'title',
           right: 'today'
       },
       defaultDate: '',
       lang: 'zh-cn',
       titleFormat:titleFormat,
       dayNamesShort: dayNamesShort,
       buttonIcons: false, // show the prev/next text
       weekMode: 'liquid',
       events:function(start,end,callback){
           if (localStorage.lang && localStorage.lang == 'en') {
               $(".fc-header-title h4").html($(".fc-header-title h4").text()+" Month")
               $('.fc-button-today').html('today')
           }
           var id=$('input[name="id"]').val();
           $.ajax({
            // url:"/api/schedules?teacheruid=3&begintime=2017-05-01&endtime=2017-05-08&status=1"
             url:"/api/schedules?status=1",
             data:{
               teacheruid:id,
               status:1
             },
             type:"get",
             success:function(data){
               console.log(data)
              var event=[];
              $.each(data.data,function(i,t){
              event.push({
                       title: t.period+' '+t.name,
                       //title:t.name,
                       start: t.begintime, // will be parsed
                       end:t.endtime,
                       id:t.id,
                       backgroundColor:"#33c7c6",
                       textColor:"#fff"

                 });
               });
                callback(event);
              for(var i=0; i<event.length;i++){
                var _id=event[i].id;

              }
             }
          });
        }
    })
 }
//搜索
$('#J_teacher_search').on('click',function(){
  var searchInput=$("#searchInput").val();
  ajaxMethod({
      url: '/api/teachers',
      type: 'get',
      data:{
        lastname:searchInput,
        mobile:searchInput
      },
      dataType: 'json',
      success: function (data) {
          studentChedule(data);
      }
    })
    function studentChedule(data) {
       // var $row = $('.row').html('');
        var $studentC = $('.studentC').html("")
        $.each(data, function (index, item) {
            function getDurationMinute(begindate, enddate) {
                if (begindate != null || enddate != null) {
                    var beginTime = +new Date(begindate.replace('T', ' '));
                    var endTime = +new Date(enddate.replace('T', ' '));
                    return (endTime - beginTime) / (60 * 1000) + '分钟';
                } else {
                    return '--';
                }
            }

            var id = item.teacheruid;
            var $tr = $('<tr class="enroll" data-uid=' + id + ' style="height:37px"></tr>').appendTo($studentC);
            var $td = $('<td ></td>').appendTo($tr);
            var $detailedinformation = $('<a  data-uid='+id+'   data-name='+item.lastname+' style="">'+item.lastname+'</a>').appendTo($td);
            $tr.append('<td>' + item.mobile + '</td>')
            $tr.find('td').css('font-size', '13px');
            $tr.on('click',function(){
            $tr.addClass('active').siblings().removeClass('active');
              var  name=$tr.find($detailedinformation).attr('data-name');
              var studentName=$('#studentName').html(name);
              if($("#calendar").html!=""){
                $("#calendar").html("");
                $('input[name="id"]').val(item.teacheruid);
                getfullCalendar();
              }
            })
          })
        }
  })

})
